<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :inline="true" :model="queryParams" ref="queryForm" size="small">
        <el-form-item label="数据资产" prop="assetId">
          <el-select v-model="queryParams.assetId" placeholder="请选择数据资产" clearable>
            <el-option
              v-for="asset in assetList"
              :key="asset.assetId"
              :label="asset.assetName + '(' + asset.assetType + ')'"
              :value="asset.assetId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="查询深度">
          <el-select v-model="queryParams.depth" placeholder="请选择深度" clearable>
            <el-option label="1层" value="1"></el-option>
            <el-option label="2层" value="2"></el-option>
            <el-option label="3层" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 血缘图谱 -->
    <div class="graph-container">
      <div id="lineageChart" class="chart" :style="{ width: '100%', height: '600px' }"></div>
    </div>

    <!-- 血缘列表 -->
    <el-table
      v-if="showTable"
      v-loading="loading"
      :data="lineageList"
      border
      fit
      highlight-current-row
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column label="上游资产" prop="sourceAssetName" align="center"></el-table-column>
      <el-table-column label="上游类型" prop="sourceAssetType" align="center"></el-table-column>
      <el-table-column label="关系类型" prop="relationType" align="center"></el-table-column>
      <el-table-column label="下游资产" prop="targetAssetName" align="center"></el-table-column>
      <el-table-column label="下游类型" prop="targetAssetType" align="center"></el-table-column>
      <el-table-column label="记录时间" prop="createTime" align="center"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getAssets, getLineageList, getLineageGraph } from '@/api/system/lineage'

export default {
  name: 'DataLineage',
  data() {
    return {
      // 资产列表
      assetList: [],
      // 血缘列表
      lineageList: [],
      // 图谱数据
      graphData: { nodes: [], links: [] },
      // 加载状态
      loading: false,
      // 查询参数
      queryParams: {
        assetId: null,
        depth: 2
      },
      // ECharts实例
      chart: null,
      // 是否显示表格
      showTable: false
    }
  },
  created() {
    this.getAssets()
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
  },
  methods: {
    /** 初始化图表 */
    initChart() {
      this.chart = echarts.init(document.getElementById('lineageChart'))
      // 初始提示
      this.chart.setOption({
        title: {
          text: '请选择数据资产并查询',
          left: 'center',
          top: 'middle'
        }
      })
    },

    /** 获取资产列表 */
    getAssets() {
      getAssets().then(response => {
        this.assetList = response.rows
      })
    },

    /** 查询血缘图谱 */
    handleQuery() {
      if (!this.queryParams.assetId) {
        this.$message.warning('请选择数据资产')
        return
      }

      this.loading = true
      getLineageGraph(this.queryParams.assetId, this.queryParams.depth).then(response => {
        this.graphData = response.data
        this.loading = false
        this.showTable = this.graphData.nodes.length > 0
        this.drawChart()
      })
    },

    /** 绘制血缘图谱 */
    drawChart() {
      const colorMap = {
        'table': '#4e79a7',
        'report': '#f28e2c',
        'file': '#59a14f'
      }

      const option = {
        tooltip: {
          formatter: function(params) {
            return `<div>
                      <div><strong>${params.data.name}</strong></div>
                      <div>类型: ${params.data.type}</div>
                      <div>描述: ${params.data.desc || '无'}</div>
                    </div>`
          }
        },
        series: [
          {
            type: 'sankey',
            layout: 'none',
            emphasis: {
              focus: 'adjacency'
            },
            data: this.graphData.nodes.map(node => ({
              name: node.name,
              itemStyle: {
                color: colorMap[node.type] || '#9c755f'
              }
            })),
            links: this.graphData.links.map(link => ({
              source: link.source,
              target: link.target,
              value: 1,
              lineStyle: {
                color: 'gradient',
                curveness: 0.5
              }
            })),
            lineStyle: {
              color: 'source',
              curveness: 0.5
            }
          }
        ]
      }

      this.chart.setOption(option)
      // 监听窗口大小变化
      window.addEventListener('resize', this.handleResize)
    },

    /** 窗口大小变化时重绘图表 */
    handleResize() {
      if (this.chart) {
        this.chart.resize()
      }
    },

    /** 重置查询条件 */
    resetQuery() {
      this.queryParams.assetId = null
      this.queryParams.depth = 2
      this.graphData = { nodes: [], links: [] }
      this.lineageList = []
      this.showTable = false
      this.initChart()
    }
  }
}
</script>

<style scoped>
.search-container {
  margin-bottom: 15px;
}
.graph-container {
  border: 1px solid #e6e6e6;
  border-radius: 4px;
  padding: 10px;
}
.chart {
  margin: 0 auto;
}
</style>
